const app = getApp();
import { request} from '../../utils/request';
import config from '../../utils/config'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        custom:wx.getMenuButtonBoundingClientRect(),
        picUrl:config.picUrl,
        formData: {album_id: 1},
        provinceList:[{code:'',name:'请选择'}],
        provinceIndex:0,
        cityList:[{code:'',name:'请选择'}],
        cityIndex:0,
        countyList:[{code:'',name:'请选择'}],
        countyIndex:0,
        settleProvinceList:[{code:'',name:'请选择'}],
        settleProvinceIndex:0,
        settleCityList:[{code:'',name:'请选择'}],
        settleCityIndex:0,
        accountTypeList:[{code:'57',name:'对公'},{code:'58',name:'对私'}],
        accountTypeIndex:0,
        btnTxt:'获取验证码',
        zuobiaoTxt:'点击获取',
        disabled:false,
        user:{},
        merRegName:'',
        merAddr:'',
        provinceCode:'',
        province:'',
        cityCode:'',
        city:'',
        countyCode:'',
        county:'',
        latitude:'',
        longtude:'',
        contactMobile:'',
        settleProvinceCode:'',
        settleProvinceName:'',
        settleCityCode:'',
        settleCityName:'',
        openningBankCode:'',
        openningBankName:'选择开户行',
        clearingBankCode:'',
        accountType:'57',
        accountName:'',
        accountNo:'',
        id_card_front_url:'',//身份证人像面
        id_card_front:'',
        id_card_behind_url:'',//身份证国徽面
        id_card_behind:'',
        business_licence_url:'',//营业执照照片
        business_licence:'',
        bank_card_url:'',//银行卡照片
        bank_card:'',
        agree_ment_url:'',//协议
        agree_ment:'',//协议
        opening_permit_url:'',//公户开户许可证
        opening_permit:'',
        checkstand_img_url:'',//收银台照片
        checkstand_img:'',
        shop_outside_img_url:'',//门头照片
        shop_outside_img:'',
        shop_inside_img_url:'',//公司内部照片
        shop_inside_img:'',
        username:'',
        password:'',
        re_password:'',
        captcha:'',
        brand_avatar:'',
        story:'',
        extension:'',
        authentication_type:3,
        wechat_no:'',
        dialogHide:true,
        agree:false
    },
    onLoad(options) {
        this.getRegion(1,'provinceList')
        this.getSettleRegion(1,'settleProvinceList')
        var user = wx.getStorageSync('userInfo')
        this.setData({user})
        if(user.is_authentication > 0){
            this.getUserData()
        }
    },
    getUserData(){
        var user = this.data.user
        request({
            url:'/miniprogram/lklpay/getUserInfo',
            data:{user_id:user.id},
            type:'get'
        }).then(res=>{
            if(res.code != 200) return false
            this.getRegion(res.data.jinjian.provinceCode,'cityList')
            this.getRegion(res.data.jinjian.cityCode,'countyList')
            this.getSettleRegion(res.data.jinjian.settleProvinceCode,'settleCityList')
            this.setData({
                merRegName:res.data.jinjian.merRegName,
                merAddr:res.data.jinjian.merAddr,
                provinceCode:res.data.jinjian.provinceCode,
                province:res.data.user.province,
                cityCode:res.data.jinjian.cityCode,
                city:res.data.user.city,
                countyCode:res.data.jinjian.countyCode,
                county:res.data.user.county,
                latitude:res.data.jinjian.latitude,
                longtude:res.data.jinjian.longtude,
                larName:res.data.jinjian.larName,
                larIdCard:res.data.jinjian.larIdCard,
                larIdCardStart:res.data.jinjian.larIdCardStart,
                larIdCardEnd:res.data.jinjian.larIdCardEnd,
                contactMobile:res.data.jinjian.contactMobile,
                settleProvinceCode:res.data.jinjian.settleProvinceCode,
                settleProvinceName:res.data.jinjian.settleProvinceName,
                settleCityCode:res.data.jinjian.settleCityCode,
                settleCityName:res.data.jinjian.settleCityName,
                openningBankCode:res.data.jinjian.openningBankCode,
                openningBankName:res.data.jinjian.openningBankName,
                clearingBankCode:res.data.jinjian.clearingBankCode,
                accountType:res.data.jinjian.accountType,
                accountName:res.data.jinjian.accountName,
                accountNo:res.data.jinjian.accountNo,
                id_card_front_url:res.data.jinjian.id_card_front_url,//身份证人像面
                id_card_front:res.data.jinjian.id_card_front,
                id_card_behind_url:res.data.jinjian.id_card_behind_url,//身份证国徽面
                id_card_behind:res.data.jinjian.id_card_behind,
                business_licence_url:res.data.jinjian.business_licence_url,//营业执照照片
                business_licence:res.data.jinjian.business_licence,
                bank_card_url:res.data.jinjian.bank_card_url,//银行卡照片
                bank_card:res.data.jinjian.bank_card,
                agree_ment_url:res.data.jinjian.agree_ment_url,//协议
                agree_ment:res.data.jinjian.agree_ment,//协议
                opening_permit_url:res.data.jinjian.opening_permit_url,//公户开户许可证
                opening_permit:res.data.jinjian.opening_permit,
                checkstand_img_url:res.data.jinjian.checkstand_img_url,//收银台照片
                checkstand_img:res.data.jinjian.checkstand_img,
                shop_outside_img_url:res.data.jinjian.shop_outside_img_url,//门头照片
                shop_outside_img:res.data.jinjian.shop_outside_img,
                shop_inside_img_url:res.data.jinjian.shop_inside_img_url,//公司内部照片
                shop_inside_img:res.data.jinjian.shop_inside_img,
                username:res.data.user.username,
                password:'',
                re_password:'',
                captcha:'',
                brand_avatar:res.data.user.brand_avatar,
                story:res.data.user.story,
                extension:res.data.user.extension,
                wechat_no:res.data.user.wechat_no
            })
            if(res.data.jinjian.accountType == '58'){
                this.setData({accountTypeIndex:1})
            }
            if(res.data.jinjian.latitude && res.data.jinjian.longtude){
                this.setData({zuobiaoTxt:'已获取'})
            }
        })
    },
    getRegion(code,field){
        wx.showLoading()
        request({
            url:"/miniprogram/lklpay/getRegion",
            data:{code},
            type:'get'
        }).then(res=>{
            wx.hideLoading()
            this.setData({[field]:res})
        })
    },
    cityPickerChange(e){
        var {field,index,list,name} = e.currentTarget.dataset
        this.setData({
            [index]:e.detail.value,
            [field]:this.data[list][e.detail.value].code
        })
        if(name){
            this.setData({[name]:this.data[list][e.detail.value].name})
        }
        if(field == 'provinceCode'){
            this.getRegion(this.data[list][e.detail.value].code,'cityList')
        }else if(field == 'cityCode'){
            this.getRegion(this.data[list][e.detail.value].code,'countyList')
        }else if(field == 'settleProvinceCode'){
            this.getSettleRegion(this.data[list][e.detail.value].code,'settleCityList')
        }
    },
    getSettleRegion(code,field){
        request({
            url:"/miniprogram/lklpay/getBankAddress",
            data:{code},
            type:'get'
        }).then(res=>{
            this.setData({[field]:res})
        })
    },
    getAddress(){
        if(this.data.zuobiaoTxt == '已获取'){
            config.toast('已获取完成，无需重复获取。',201,()=>{})
            return false
        }
        wx.showLoading()
        wx.getFuzzyLocation({
            type: 'wgs84',
            success: res => {
                wx.hideLoading()
                if (res.errMsg != 'getFuzzyLocation:ok') {
                    wx.showToast({title: res.errMsg, icon: 'none'})
                    return false
                }
                this.setData({
                    latitude:res.latitude,
                    longtude:res.longitude,
                    zuobiaoTxt:'已获取'
                })
            }
        })
    },
    naviGetBank(){
        if(!this.data.settleCityCode){
            config.toast('请先选择开户行所在城市',201,()=>{})
            return false
        }
        wx.navigateTo({url:'/pages/get_bank/get_bank?code='+this.data.settleCityCode})
    },
    uploadImg(e) {
        var field = e.currentTarget.dataset.field
        if('id' in e.currentTarget.dataset){
            var id = e.currentTarget.dataset.id
            this.upload(1,{imgType:id.toUpperCase()}, res => {
                if (res.code != 200) {
                    wx.showToast({title: res.msg, icon: 'none'})
                    return false
                }
                this.setData({
                    [field]: res.data.src,
                    [id]:res.data.url
                })
            })
        }else{
            this.upload(1,{imgType:'none'}, res => {
                console.log(e)
                console.log(res)
                if (res.code != 200) {
                    wx.showToast({title: res.msg, icon: 'none'})
                    return false
                }
                this.setData({[field]: res.data.src})
            })
        }
    },
    delImg(e) {
        this.setData({[e.currentTarget.dataset.field]: ''})
        if('id' in e.currentTarget.dataset){
            this.setData({[e.currentTarget.dataset.id]: ''})
        }
    },
    upload(count,formData, callback) {
        wx.showLoading()
        wx.chooseImage({
            count: count,
            sizeType: ['compressed', 'original'],
            success: res => {
                for (let v of res.tempFiles) {
                    wx.uploadFile({
                        url: config.url + '/miniprogram/lklpay/uploadFile',
                        filePath: v.path,
                        name: 'file',
                        formData: formData,
                        success: res => {
                            wx.hideLoading()
                            callback(JSON.parse(res.data))
                        }
                    })
                }
            },
            fail: err => {
                wx.hideLoading()
            }
        })
    },
    submit(e){
        if(!this.data.agree){
            config.toast('请先阅读并同意《入驻协议》。',201,()=>{})
            return false
        }
        var user = this.data.user
        if(user.is_authentication == 1){
            config.toast('正在审核中，请勿重复提交认证信息。',201,()=>{})
            return false;
        }
        if(user.is_authentication == 2){
            config.toast('审核已通过，请勿重复提交认证信息。',201,()=>{})
            return false;
        }
        var member = e.currentTarget.dataset.member
        var {merRegName,merAddr,provinceCode,cityCode,countyCode,province,city,county,latitude,longtude,contactMobile,id_card_front_url,id_card_front,id_card_behind_url,id_card_behind,business_licence_url,business_licence,checkstand_img_url,checkstand_img,shop_outside_img_url,shop_outside_img,shop_inside_img_url,shop_inside_img,captcha,brand_avatar,story,extension,authentication_type,wechat_no} = this.data
        if(!merRegName || !merAddr || !provinceCode || !cityCode || !countyCode || !latitude || !longtude || !contactMobile || !id_card_front_url || !id_card_front || !id_card_behind_url || !id_card_behind || !business_licence_url || !business_licence || !captcha || !province || !city || !county || !wechat_no){
            config.toast("上述所有选项均为必填或必传项，否则将导致审核失败！",201,()=>{})
            return false
        }
        var obj = {merRegName,merAddr,provinceCode,cityCode,countyCode,province,city,county,latitude,longtude,contactMobile,id_card_front_url,id_card_front,id_card_behind_url,id_card_behind,business_licence_url,business_licence,checkstand_img_url,checkstand_img,shop_outside_img_url,shop_outside_img,shop_inside_img_url,shop_inside_img,captcha,brand_avatar,story,extension,authentication_type,wechat_no,user_id:this.data.user.id}
        if(member == 0) {
            this.setData({
                dialogHide:false
            })
        }else{
            this.registor(obj,member)
        }
    },
    refusePay(){
        var {merRegName,merAddr,provinceCode,cityCode,countyCode,province,city,county,latitude,longtude,contactMobile,id_card_front_url,id_card_front,id_card_behind_url,id_card_behind,business_licence_url,business_licence,checkstand_img_url,checkstand_img,shop_outside_img_url,shop_outside_img,shop_inside_img_url,shop_inside_img,captcha,brand_avatar,story,extension,authentication_type,wechat_no} = this.data
        var member = 0,
            obj = {merRegName,merAddr,provinceCode,cityCode,countyCode,province,city,county,latitude,longtude,contactMobile,id_card_front_url,id_card_front,id_card_behind_url,id_card_behind,business_licence_url,business_licence,checkstand_img_url,checkstand_img,shop_outside_img_url,shop_outside_img,shop_inside_img_url,shop_inside_img,captcha,brand_avatar,story,extension,authentication_type,wechat_no,user_id:this.data.user.id}
        this.registor(obj,member)
    },
    agreePay(){
        var {merRegName,merAddr,provinceCode,cityCode,countyCode,province,city,county,latitude,longtude,contactMobile,id_card_front_url,id_card_front,id_card_behind_url,id_card_behind,business_licence_url,business_licence,checkstand_img_url,checkstand_img,shop_outside_img_url,shop_outside_img,shop_inside_img_url,shop_inside_img,captcha,brand_avatar,story,extension,authentication_type,wechat_no} = this.data
        var member = 1,
            obj = {merRegName,merAddr,provinceCode,cityCode,countyCode,province,city,county,latitude,longtude,contactMobile,id_card_front_url,id_card_front,id_card_behind_url,id_card_behind,business_licence_url,business_licence,checkstand_img_url,checkstand_img,shop_outside_img_url,shop_outside_img,shop_inside_img_url,shop_inside_img,captcha,brand_avatar,story,extension,authentication_type,wechat_no,user_id:this.data.user.id}
        this.registor(obj,member)
    },
    getCode(){
        var phone = this.data.contactMobile
        if(!phone){
            config.toast("请先填写手机号码后再获取验证码",201,res=>{})
            return false
        }
        this.setData({disabled:true})
        var num = 60;
        var timer = setInterval(()=>{
            if(num <= 1){
                this.setData({btnTxt:'获取验证码',disabled:false})
                clearInterval(timer)
            }else{
                num--;
                this.setData({btnTxt:num+"秒后重试"})
            }
        },1000)
        wx.showLoading()
        request({
            url:"/miniprogram/system/sendSmsCode",
            data:{openid:this.data.user.openid,phone:phone},
        }).then(res=>{
            wx.hideLoading()
            config.toast(res.msg,201,()=>{})
        })
    },
    changeValue(e){
        var field = e.currentTarget.dataset.field,
            value = e.detail.value
        this.setData({[field]:value})
    },
    registor(obj,member){
        wx.showLoading()
        request({
            url:'/miniprogram/lklpay/renzheng',
            data:{...obj},
            type:'post',
        }).then(res=>{
            wx.hideLoading()
            config.toast(res.msg,201,rel=>{
                this.setData({dialogHide:true})
                if(res.code == 200){
                    console.log(member)
                    wx.clearStorageSync()
                    wx.setStorageSync('userInfo',res.data)
                    if(member == 1){
                        console.log(1)
                        setTimeout(()=>{wx.navigateTo({url: '/pages/member_recharge/member_recharge'})},1500)
                    }else if(res.code == 200 && member == 0){
                        console.log(2)
                        setTimeout(()=>{wx.navigateBack()},1500)
                    }
                    console.log(3)
                }
            })
        })
    },
    checkbox(e){
        if(e.detail.value.length > 0){
            this.setData({wechat_no:this.data.contactMobile})
        }else {
            this.setData({wechat_no:''})
        }
    },
    radioChange(e){
        if(e.detail.value.length == 1){
            this.setData({agree:true})
        }else{
            this.setData({agree:false})
        }
    }
})